package sfmc.biz.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import sfmc.biz.daoImpl.SfmcEmpDaoImpl;
import sfmc.biz.service.SfmcCustomerService;
import sfmc.biz.service.SfmcEmpService;
import sfmc.biz.vo.Sfmc_TB_CUSTOMER;
import sfmc.biz.vo.Sfmc_TB_EMPLOYEE;

@Controller
public class SfmcEmpController {
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	@Autowired 
	private SfmcEmpService sfmcEmpService;
	
	@RequestMapping(value="/emp_list.do")
	public ModelAndView empList(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_EMPLOYEE sfmc_TB_EMPLOYEE){
		
		List<Sfmc_TB_EMPLOYEE> empList = sfmcEmpService.getEmpList(sfmc_TB_EMPLOYEE);
		Gson gson = new Gson();
		
		mav.addObject("empList", gson.toJson(empList));
		mav.addObject("empdata", sfmc_TB_EMPLOYEE);
		mav.setViewName("/emp/emp_list");
		
		return mav;
	}
	
	
	@RequestMapping(value="/emp_save.do")
	public ModelAndView empInsert(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_EMPLOYEE sfmc_TB_EMPLOYEE){
		
		
		int returnVal = -1;
		String type = "";
		
		if(sfmc_TB_EMPLOYEE.getDataEmpIdx() == 0){
			returnVal = sfmcEmpService.insertEmp(sfmc_TB_EMPLOYEE);
			type = "C";
		}
		else{
			returnVal = sfmcEmpService.updateEmp(sfmc_TB_EMPLOYEE);
			type = "U";
		}
		
		
		mav.addObject("empdata", sfmc_TB_EMPLOYEE);
		mav.addObject("returnVal", returnVal);
		mav.addObject("type", type);
		mav.setViewName("/emp/emp_result");
		
		return mav;
	}
	
	@RequestMapping(value="/emp_delete.do")
	public ModelAndView empDelete(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_EMPLOYEE sfmc_TB_EMPLOYEE){
		
		
		int returnVal = -1;
		String type = "D";
		
		if(sfmc_TB_EMPLOYEE.getDataEmpIdx() != 0){
			returnVal = sfmcEmpService.deleteCustomer(sfmc_TB_EMPLOYEE);
		}
		
		mav.addObject("empdata", sfmc_TB_EMPLOYEE);
		mav.addObject("returnVal", returnVal);
		mav.addObject("type", type);
		mav.setViewName("/emp/emp_result");
		
		return mav;
	}
	
	@RequestMapping(value="/emp_excel.do")
	public void listsToExcel(HttpServletResponse resp, OutputStream ost,ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_EMPLOYEE sfmc_TB_EMPLOYEE) throws Exception{
		
		FileOutputStream fos = null;
		
		List<Sfmc_TB_EMPLOYEE> excelList = sfmcEmpService.getEmpList(sfmc_TB_EMPLOYEE);
		resp.setHeader("Content-Disposition", "attachment; filename=empList.xls");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet firstSheet  = workbook.createSheet("sheet1");
		
		for(int i=0; i<excelList.size(); i++){
			
			HSSFRow rowHeader = firstSheet.createRow(0);
			HSSFCell headerA = rowHeader.createCell(0);
			HSSFCell headerB = rowHeader.createCell(1);
			headerA.setCellValue(new HSSFRichTextString("EmpIdx"));
			headerB.setCellValue(new HSSFRichTextString("사원명"));
			
			sfmc_TB_EMPLOYEE = excelList.get(i);
			HSSFRow rowA = firstSheet.createRow(i);
			HSSFCell cells1 = rowA.createCell(0); 
			HSSFCell cells2 = rowA.createCell(1);
			cells1.setCellValue(sfmc_TB_EMPLOYEE.getEmpIdx());
			cells2.setCellValue(sfmc_TB_EMPLOYEE.getEmpName());
		}
		
		 try{
	        	workbook.write(ost);
	        	ost.flush();
	        	fos = new FileOutputStream(new File("C:/empList.xls"));
	        	workbook.write(fos);
	        }
	        catch(Exception e){
	        	log.fatal(e.getMessage()+"\n");
	        	log.fatal(e.getStackTrace());
	        }
	        finally{
	        	if(fos != null){
	        		try{
		        		fos.flush();
		                fos.close();
	        		}
	        		catch(IOException e){
	        			log.fatal(e.getMessage());
	        		}
	        	}
	        }
		
	}//end of listsToExcel
		
	
	
	
	
	
	
	
	
}



